#!/bin/bash
#
#	Release script for the eloy backup system.
#
#	Copyright (c) 2007-2008 Maximilian Antoni. All rights reserved.
#
#	This software is licensed as described in the file LICENSE.txt, which you
#	should have received as part of this distribution. The terms are also
#	available at http://svn.evaserver.com/eloy/trunk/LICENSE.txt.
#

DIRECTORY_PREFIX="eloy-"

copyResource() {
	cp $1 "$DIRECTORY/$1"
}

prepareRelease() {
	DIRECTORY="$DIRECTORY_PREFIX$VERSION"
	if [ -e "$DIRECTORY" ]; then
		echo "\"$DIRECTORY\" already exists."
		echo
		exit 1
	fi
	if [ -e "$DIRECTORY.tar.gz" ]; then
		echo "\"$DIRECTORY.tar.gz\" already exists."
		echo
		exit 1
	fi
	
	# Create the release directory.
	mkdir "$DIRECTORY"

	# Filter eloy.sh for the version number, replace with the new version and
	# write to the release directory.
	sed -e "s/^VERSION=\".*\"$/VERSION=\"$VERSION\"/" eloy.sh > "$DIRECTORY/eloy.sh"
	chmod +x "$DIRECTORY/eloy.sh"

	# Copy resources:
	copyResource "eloy.cf"
	copyResource "LICENSE.txt"
	copyResource "eloy-backup.scpt"
	copyResource "eloy-compress.scpt"

	# Create archive:
	tar -czf "$DIRECTORY.tar.gz" "$DIRECTORY"
}

findReleaseVersion() {
	FILES=`ls | grep "$DIRECTORY_PREFIX"`
	for FILE in $FILES
	do
		if [ "${FILE:(-7)}" == ".tar.gz" ]; then
			PREFIX_LENGTH=${#DIRECTORY_PREFIX}
			SUFFIX_START=${#FILE}
			let "SUFFIX_START-=$PREFIX_LENGTH"
			let "SUFFIX_START-=7"
			RELEASE_VERSION="${FILE:$PREFIX_LENGTH:$SUFFIX_START}"
		fi
	done
	if [ -z $RELEASE_VERSION ]; then
		echo "Release file not found."
		echo
		exit 1
	fi
}

cleanup() {
	if [ -e "$DIRECTORY_PREFIX$RELEASE_VERSION" ]; then
		rm -r "$DIRECTORY_PREFIX$RELEASE_VERSION"
	fi
	rm "$DIRECTORY_PREFIX$RELEASE_VERSION.tar.gz"
}

performRelease() {
	VERSION="$VERSION-SNAPSHOT"
	
	findReleaseVersion
	
	rm "eloy.sh"
	mv "$DIRECTORY_PREFIX$RELEASE_VERSION/eloy.sh" "eloy.sh"
	rm -r "$DIRECTORY_PREFIX$RELEASE_VERSION"
	
	# Commit:
	svn add "$DIRECTORY_PREFIX$RELEASE_VERSION.tar.gz"
	svn ci -m "[Preparing release $RELEASE_VERSION]"

	# Create tag:
	svn cp -m "[Performing release $RELEASE_VERSION]" "http://svn.evaserver.com/eloy/trunk" "http://svn.evaserver.com/eloy/tags/$RELEASE_VERSION"
	
	# Remove tar.gz and prepare next version:
	sed -e "s/^VERSION=\"$RELEASE_VERSION\"$/VERSION=\"$VERSION\"/" eloy.sh > "eloy.sh.temp"
	rm "eloy.sh"
	mv "eloy.sh.temp" "eloy.sh"
	chmod +x "eloy.sh"
	
	svn rm "$DIRECTORY_PREFIX$RELEASE_VERSION.tar.gz"
	svn ci -m "[Preparing next development iteration $VERSION]"
}

# Check arguments.
if [ -z $1 ]; then
	echo "Please provide a command (prepare | perform | cleanup)."
	echo
	exit 1
fi

checkVersion() {
	if [ -z $1 ]; then
		echo "Please provide the version number to use."
		echo
		exit 1
	else
		VERSION="$1"
	fi
}

case "$1" in
	"prepare" )
		checkVersion $2
		prepareRelease
	;;
	"perform" )
		checkVersion $2
		performRelease
	;;
	"cleanup" )
		findReleaseVersion
		cleanup
	;;
	* )
	echo "Invalid command. Use prepare, perform or cleanup."
	echo
	exit 1
esac

exit 0
